import { Controller, Post, UseInterceptors, UploadedFile, Get, Body, Query, Headers } from '@nestjs/common';
import { UploadService } from "./upload.service"
import { FileInterceptor, FilesInterceptor } from "@nestjs/platform-express"
import { createWriteStream } from 'fs';
import { join } from 'path';
const path = require("path")
import multer = require('multer');
@Controller('upload')
export class UploadController {
    constructor(private uploadService: UploadService) { }

    @Post("album")
    @UseInterceptors(FileInterceptor("file"))
    upload(@Body() body, @UploadedFile() file) {
        // console.log(file, "file")
        // console.log(body, "body")
        return {
            code: "200",
            mes: "图片上传成功",
            data: file.filename

        }
    }

    @Post("addvideo")
    @UseInterceptors(FileInterceptor("video",{
        storage:multer.diskStorage({
            destination: (req, file, cb) => {
                cb(null, path.join(__dirname, "../images"))
            },
            filename: (req, file, cb) => {
                cb(null, Date.now() + path.extname(file.originalname))
            },
        }),
    }))
    addVideo(@Headers() body, @UploadedFile() file) {
        console.log(body)
        console.log(file,"dfefe")
        // const writeStream = createWriteStream(join(__dirname, '../images', `${Date.now()}-${file.originalname}`))
        // writeStream.write(file.buffer);

        return {
            code: 200,
            mes: "视频上传成功"
        }
    }

}
